package com.db.concurrency.tracker;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Alexander Kotov (kotov.alex.22@gmail.com)
 */
public class JavaMonitorPatternTracker {
    private final Map<String, MutablePoint> map;

    public JavaMonitorPatternTracker(Map<String, MutablePoint> map) {
        this.map = deepCopy(map);
    }

    public MutablePoint getLocation(String id) {
        MutablePoint point = null;
        point = map.get(id);
        if (point != null) {
            return new MutablePoint(point);
        }
        return null;
    }

    public void setLocation(String id, MutablePoint p) {
        map.put(id, new MutablePoint(p));
    }

    public Map<String, MutablePoint> getLocations() {
        return deepCopy(map);
    }

    private static Map<String, MutablePoint> deepCopy(final Map<String, MutablePoint> data) {
        Map<String, MutablePoint> copy = new HashMap<String, MutablePoint>();
        for (Map.Entry<String, MutablePoint> entry : data.entrySet()) {
            copy.put(entry.getKey(), entry.getValue());
        }
        return copy;
    }
}
